package es.uab.sound.graph;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import es.uab.sound.utils.Settings;

public class LineGraph {

	private GraphicalView view;

	private TimeSeries dataSet = new TimeSeries("Noise");
	private XYMultipleSeriesDataset mDataSet = new XYMultipleSeriesDataset();
	private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

	public GraphicalView getView(Context context) {
		prepareChartSettings(new Settings(context));
		view = ChartFactory.getLineChartView(context, mDataSet, mRenderer);
		//.view = ChartFactory.getTimeChartView(context, mDataSet, mRenderer, "dd/MM");
		return view;
	}

	public void add(PointTime point) {
		dataSet.add(point.getX(), point.getY());
	}

	private void prepareChartSettings(Settings settings) {

		mDataSet.addSeries(dataSet);
		mRenderer.setYAxisMin(settings.getYMin());
		mRenderer.setYAxisMax(settings.getYMax());

		//Show 
		mRenderer.setZoomEnabled(settings.isShowZoom());
		mRenderer.setShowAxes(settings.isShowAxes());
		mRenderer.setShowGrid(settings.isShowGrid());
		mRenderer.setShowLegend(settings.isShowLegend());
		mRenderer.setZoomButtonsVisible(settings.isShowZoom());

		//Font Size
		mRenderer.setYLabelsPadding(17);
		mRenderer.setAxisTitleTextSize(10);
		mRenderer.setLabelsTextSize(10);

		//Margins
		mRenderer.setMargins(new int[] { 20, 30, 20, 20 });

		//Colors
		mRenderer.setApplyBackgroundColor(true);
		mRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
		mRenderer.setBackgroundColor(Color.parseColor("#fff3f3f3"));
		mRenderer.setAxesColor(Color.DKGRAY);
		mRenderer.setLabelsColor(Color.LTGRAY);

		XYSeriesRenderer renderer = new XYSeriesRenderer();
		renderer.setColor(Color.parseColor("#33b5e5"));
		mRenderer.addSeriesRenderer(renderer);
	}

}
